/*
 * @Date: 2020-09-23 10:17:43
 * @LastEditors: 看法是豆腐
 * @LastEditTime: 2020-09-23 21:44:55
 * @Description:
 */
export function jsonToStr(json) {
    // 两种传参方式：json（键值对）转string（）
    let str = ''
    for (let key in json) {
        if (json[key] !== undefined) {
            str += '&' + key + '=' + json[key]
        }
    }
    return str.substring(1)
}
export function reFresh(vue, components) {
    // 刷新子组件
    components.show = false
    vue.$nextTick(() => {
        components.show = true
    })
}
export function jsonToTree(data, config) {
    let id = config.id || 'id'; let pid = config.pid || 'pid'; let children = config.children || 'children'
    let idMap = []; let jsonTree = []
    data.forEach(function (v) {
        idMap[v[id]] = v
    })
    data.forEach(function (v) {
        let parent = idMap[v[pid]]
        if (parent) {
            !parent[children] && (parent[children] = [])
            parent[children].push(v)
        } else {
            jsonTree.push(v)
        }
    })
    return jsonTree
}

/**
 * 判断空对象
 * @param {Object} obj
 */
export function isEmptyObj(obj) {
    if (Object.keys(obj).length === 0) {
        return false // 如果为空,返回false
    }
    return true
}

export function goToPage(hash, params, htmlPage) {
    if (params) {
        params = '?' + jsonToStr(params)
    } else {
        params = ''
    }
    if (htmlPage != undefined && htmlPage != null) {
        window.location.hash = `#${htmlPage}${hash}${params}`
    } else if (isDev()) {
        window.location.hash = `#${hash}${params}`
    } else {
        window.location.hash = `#vue/${hash}${params}`
    }
}

export function openNewTab(hash, params) {
    if (params) {
        params = '?' + jsonToStr(params)
    } else {
        params = ''
    }
    let url = '';
    if (isDev()) {
        url = `#${hash}${params}`
    } else {
        url = `#vue/${hash}${params}`
    }
    window.open(url);
}

export function isDev() {
    return window.location.port == 8011
}
/**
 * 获取url上的参数
 * @param {String} variable
 */
export function getUrlVariable(variable) {
    //获取Url 参数
    var GET = {};
    var param = window.location.hash.split("?")[1] == undefined ? '' : window.location.hash.split("?")[1];
    if (param != '') {
        var strarr = param.split("&");
        for (let i = 0; i < strarr.length; i++) {
            var xx = strarr[i];
            var mykey = xx.split('=')[0];
            var myvalue = xx.split('=')[1];
            GET[mykey] = myvalue;
        }
    }
    return GET[variable];
}
/**
 * 获取当前时间
 * @param {*} variable
 */
export function formatTime(time, fmt) {
    const date = new Date(time);
    const o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'H+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds(),
        'q+': Math.floor((date.getMonth() + 3) / 3),
        S: date.getMilliseconds(),
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(
            RegExp.$1,
            (date.getFullYear() + '').substr(4 - RegExp.$1.length)
        );
    for (const k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
            fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length === 1
                    ? o[k]
                    : ('00' + o[k]).substr(('' + o[k]).length)
            );
        }
    }
    return fmt;
}
